Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: migrate to ESM #8178

Merged
merged 24 commits into from
May 21, 2022
Merged

feat!: migrate to ESM #8178

merged 24 commits into from
May 21, 2022

Conversation

antfu
Copy link
Member

@antfu antfu commented May 14, 2022

close #5839

Breaking changes

  • vite now has type: module
  • .js under dist/ are now ESM
  • index.cjs is created for CJS compactiblity, which proxies ESM APIs without a full CJS build.
  • formatPostcssSourceMap is now async
  • The following APIs are no longer avaliable in CJS build (dynamic import is needed to use in CJS)
    • resolvePackageEntry
    • resolvePackageData

@antfu antfu added this to the 3.0 milestone May 14, 2022
packages/vite/index.cjs Outdated Show resolved Hide resolved
@antfu antfu marked this pull request as ready for review May 15, 2022 00:23
@antfu
Copy link
Member Author

antfu commented May 15, 2022

➜ ls -l *.tgz
-rw-r--r--  1 antfu  staff  1036930 May 15 09:11 vite-3.0.0-alpha.0-cjs.tgz
-rw-r--r--  1 antfu  staff  1077340 May 15 08:32 vite-3.0.0-alpha.0-esm.tgz

➜ ls -lh *.tgz
-rw-r--r--  1 antfu  staff   1.0M May 15 09:11 vite-3.0.0-alpha.0-cjs.tgz
-rw-r--r--  1 antfu  staff   1.0M May 15 08:32 vite-3.0.0-alpha.0-esm.tgz

Bundle size diff is not significant.

@antfu antfu changed the title chore: migrate core to esm feat!: migrate to ESM May 15, 2022
@patak-dev patak-dev added p2-nice-to-have Not breaking anything but nice to have (priority) p3-significant High priority enhancement (priority) and removed p2-nice-to-have Not breaking anything but nice to have (priority) labels May 18, 2022
@brillout
Copy link
Contributor

This is a great move since it effectively gets rid of the ESM-only error for Vite users. (This is the #1 error my users get, by far. And many of them are quite confused about it.)

We can say whatever we want about Node.js's ESM-only strategy and the folks like node-fetch who shipped ESM-only packages, but it did work out like a charm :-).

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After building, it looks like the raw individual .d.ts file are still left in dist/node/*. Using api-extractor I think these are bundled and can be removed. I'm not sure what's causing it though.

packages/vite/package.json Show resolved Hide resolved
packages/vite/package.json Outdated Show resolved Hide resolved
packages/vite/src/node/config.ts Show resolved Hide resolved
@antfu
Copy link
Member Author

antfu commented May 20, 2022

Tested in VitePress and it works!

@patak-dev patak-dev mentioned this pull request May 20, 2022
4 tasks
"require": "./index.cjs"
},
"./client": {
"types": "./client.d.ts"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ./client export is needed when using the new nodenext module resolution in the upcoming TypeScript 4.7 release.

As it stands now, using Vite 2.9.9 with typescript 4.7.1-rc + module: "nodenext" does not work because the compiler cannot access client.d.ts as it is not explicitly exported.

/// <reference types="vite/client" />

Looking forward to this getting into the 3.0.0 release.

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@antfu I think you can merge it and release a new alpha with it when you see fit.

@antfu
Copy link
Member Author

antfu commented May 21, 2022

Let's merge it to try in the next alpha!

@antfu antfu merged commit 76fdc27 into main May 21, 2022
@antfu antfu deleted the feat/esm branch May 21, 2022 05:38
Comment on lines +11 to +12
"main": "./dist/node/index.js",
"module": "./dist/node/index.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could remove these 2 fields since Vite 3 supports Node 14+, Node 12+ supports exports field and exports takes priority than main and module so Node.js will never reach these 2 fields.

"types": "./client.d.ts"
},
"./terser": {
"require": "./dist/node-cjs/terser.cjs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS complains here as missing declaration file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What version of TS and what “module” and/or “moduleResolution” settings are you using in your tsconfig.json file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried with TS 4.6 (node) and 4.7-rc1 (nodenext) with "module" being "esnext"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change p3-significant High priority enhancement (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Kill CJS
6 participants